home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 14.3 KB | 527 lines | [TEXT/MPS ] |
- // UPopup.cp
- // Copyright © 1988-1991 by Apple Computer Inc. All rights reserved.
-
- #ifndef __UGEOMETRY__
- #include <UGeometry.h>
- #endif
-
- #ifndef __CONTROLS__
- #include <Controls.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include <UMacAppGlobals.h>
- #endif
-
- #ifndef __UFAILURE__
- #include <UFailure.h>
- #endif
-
- #ifndef __UMENUMGR__
- #include <UMenuMgr.h>
- #endif
-
- #ifndef __UPOPUP__
- #include <UPopup.h>
- #endif
-
- //--------------------------------------------------------------------------------------------------
- const short kMAPopupRsrcID = 128; // The rsrc id of the Popup CDEF supplied with MacApp.
-
- //--------------------------------------------------------------------------------------------------
-
- // The following data structure is a private data structure used by the popup CDEF. It lives as
- // a privHdl off the control handle's contrlData field. The first two fields of the private data
- // structure are "public" but the rest are private.
- struct popupPrivateData
- {
- MenuHandle mHandle; // the popup's menu handle
- short mID; // the popup's menu id
- long mLastResult;
- SignedByte* mPrivate;
- };
-
-
- typedef popupPrivateData* popupPrivateDataPtr;
- typedef popupPrivateDataPtr* popupPrivateDataHandle;
-
- //--------------------------------------------------------------------------------------------------
-
- ResType pUseResType;
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgOpen
-
- pascal void TPopup::Initialize(void) // override
- {
- inherited::Initialize();
-
- fDefChoice = mPopupHit;
-
- fItemOffset = 0; // ??? Is this the best default?
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgOpen
-
- pascal void TPopup::IPopup(TView* itsSuperView,
- const VPoint& itsLocation,
- const VPoint& itsSize,
- SizeDeterminer itsHSizeDet,
- SizeDeterminer itsVSizeDet,
- short itsMenuID,
- // rsrc ID of 'MENU' or 'CMNU' rsrc
- short itsCurrentItem,
- short itsItemOffset,
- // width of the popup title area
- const Str255& itsLabel,
- /* Style */ short itsStyle,
- // title's style
- short itsJust,
- // title's justification
- Boolean useAddResMenu,
- // TRUE if want to AddResMenu (can use
- // this option iff menuID != kNoResource)
- ResType useAddResMenuResType) // used if useAddResMenu is TRUE
-
- {
- short itsProcID = popupMenuProc;
-
- if (!gConfiguration.hasPopupCDEF)
- itsProcID = 16 * kMAPopupRsrcID; // use MAPopup if System one isn't present
-
- itsProcID += popupUseWFont; // always use window font
-
- pUseResType = ' ';
- if (useAddResMenu)
- {
- itsProcID += popupUseAddResMenu;// set if popup uses AddResMenu
- pUseResType = useAddResMenuResType;
- }
-
- // initialize the popup
- ICtlMgr(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet, itsLabel, GetPopupTitleStyle(itsStyle, itsJust), itsMenuID, itsItemOffset, itsProcID);
-
- // set up the rest of the instance variables
- fItemOffset = itsItemOffset;
-
- // SetCurrentItem(itsCurrentItem, kDontRedraw);
- fItsCurrentItem = itsCurrentItem;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgOpen
-
- pascal void TPopup::IRes(TDocument* ,
- TView* itsSuperView,
- Ptr& itsParams) // override
- {
- Str255 itsLabel("");
- short itsPopupTitleStyle = popupTitleNoStyle;
- short itsProcID;
- short itsMenuID;
- short itsItemOffset;
- short itsCurrentItem;
- MenuHandle itsMenuHandle = NULL;
-
- inherited::IRes(NULL, itsSuperView, itsParams);
-
- {
- PopupTemplate& templateData = *((PopupTemplate *) itsParams);
-
- itsMenuID = templateData.rsrcID;
- itsItemOffset = templateData.itemOffset;
- itsProcID = popupMenuProc + popupUseWFont; // always use window font
- itsCurrentItem = templateData.currentItem;
-
- pUseResType = ' '; // !!! fix this when the PopupTemplate is updated
-
- // uncomment this when the PopupTemplate is updated
- // itsPopupTitleStyle = GetPopupTitleStyle(templateData.itsStyle, templateData.itsJust);
- }
-
- // get the menu label
- itsMenuHandle = MAGetMenu(itsMenuID);
- if (itsMenuHandle)
- CopyStr255((**itsMenuHandle).menuData, (Ptr) &itsLabel);
-
- // initialize the popup
- VRect itsBounds;
- this->ControlArea(itsBounds);
- this->CreateCMgrControl(itsBounds, itsLabel, itsPopupTitleStyle, itsMenuID, itsItemOffset, itsProcID);
-
- // set up the rest of the instance variables
- fItemOffset = itsItemOffset;
-
- // SetCurrentItem(itsCurrentItem, kDontRedraw);
- fItsCurrentItem = itsCurrentItem;
-
- OffsetPtr(itsParams, sizeof(PopupTemplate));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgOpen
-
- pascal short TPopup::GetPopupTitleStyle(/* Style */ short itsStyle,
- short itsJust)
- {
- short popupTitleStyle;
-
- // set up justification value
- if (itsJust == teJustCenter)
- popupTitleStyle = popupTitleCenterJust;
- else if (itsJust == teJustRight)
- popupTitleStyle = popupTitleRightJust;
- else
- popupTitleStyle = popupTitleLeftJust; // default value.
-
- // set up style value
- if (itsStyle == normal)
- popupTitleStyle += popupTitleNoStyle;
- else
- {
- if (IN(itsStyle, bold))
- popupTitleStyle += popupTitleBold;
- if (IN(itsStyle, italic))
- popupTitleStyle += popupTitleItalic;
- if (IN(itsStyle, underline))
- popupTitleStyle += popupTitleUnderline;
- if (IN(itsStyle, outline))
- popupTitleStyle += popupTitleOutline;
- if (IN(itsStyle, shadow))
- popupTitleStyle += popupTitleShadow;
- if (IN(itsStyle, condense))
- popupTitleStyle += popupTitleCondense;
- if (IN(itsStyle, extend))
- popupTitleStyle += popupTitleExtend;
- }
-
- return popupTitleStyle;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgOpen
-
- pascal void TPopup::CreateCMgrControl(const VRect& itsBounds,
- const Str255& itsTitle,
- long itsVal,
- long itsMin,
- long itsMax,
- short itsProcID)// override
- {
- GrafPtr itsPort;
- ControlHandle aCMgrControl;
- FailInfo fi;
- TextStyle aTextStyle;
-
- itsPort = GetGrafPort();
- if (!itsPort)
- itsPort = gWorkPort;
-
- // set the textstyle to use when creating the popup
- // aTextStyle = fTextStyle;
- // SetPortTextStyle(aTextStyle);
-
- // create the control
- if (fi.Try())
- {
- Rect qdRect;
- this->ViewToQDRect(itsBounds, qdRect);
- aCMgrControl = NewControl(itsPort, qdRect, itsTitle, FALSE, (short)itsVal, (short)itsMin, (short)itsMax, itsProcID, ((long)pUseResType));
- FailNIL(aCMgrControl);
- fi.Success();
- }
- else // Recover
- {
- this->Free();
- fi.ReSignal();
- }
-
- SetCRefCon(aCMgrControl, ((long)this)); // Set refCon here, it has a special value at
- // NewControl time - the ResType for the case
- // in which the popup's variation code has
- // popupUseAddResMenu.
-
- // Keep control off Window's control list b/c the Window Mgr adds the regions of controls in
- // the control list to the update region (see NOTE under DrawControls in Inside Mac volume I)
- // generating an update event.
-
- ((WindowPeek)itsPort)->controlList = (*((WindowPeek)itsPort)->controlList)->nextControl;
-
- fCMgrControl = aCMgrControl;
- DimState(fDimmed, kDontRedraw);
-
- SetCMgrVisibility(itsPort != gWorkPort);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal TObject* TPopup::Clone(void) // override
- {
- TPopup* aPopup = (TPopup *)(inherited::Clone());
-
- return aPopup;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteRes
-
- pascal void TPopup::WRes(ViewRsrcHandle theResource,
- Ptr& itsParams) // override
- {
- inherited::WRes(theResource, itsParams);
- // !!! needs to be fleshed out
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAWriteRes
-
- pascal void TPopup::WriteRes(ViewRsrcHandle theResource,
- Ptr& itsParams) // override
- {
- gWResSignature = 'popp';
- gWResType = "TPopup";
- WRes(theResource, itsParams);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::Draw(const VRect& area) // override
- {
- TextStyle aTextStyle;
-
- this->SetCurrentItem(fItsCurrentItem, kDontRedraw);
-
- // set the textstyle to use when creating the popup
- aTextStyle = fTextStyle;
- SetPortTextStyle(aTextStyle);
-
- inherited::Draw(area);
- }
-
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::DoMouseCommand(VPoint& theMouse,
- TToolboxEvent* event,
- Point hysteresis)// override
- {
- TextStyle aTextStyle;
-
- // set the textstyle to use when creating the popup
- aTextStyle = fTextStyle;
- SetPortTextStyle(aTextStyle);
-
- inherited::DoMouseCommand(theMouse, event, hysteresis);
-
- fItsCurrentItem = this->GetCurrentItem();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::AttachMenuHandle(MenuHandle itsMenuHandle)
- {
- MenuHandle currentMenuHandle;
-
- // first, a quick safety check to make sure that we do nothing if we are trying to install
- // a menu that is already installed
- currentMenuHandle = GetMenuHandle();
- if (currentMenuHandle != itsMenuHandle)
- {
- if (currentMenuHandle) // first, release the old menuhandle
- {
- // There are 3 cases that we need to account for here:
- // 1. if fMenuHandle is a converted 'CMNU' resource => leave MenuHandle alone;
- // 2. if fMenuHandle is a 'MENU' resource => call ReleaseResource;
- // 3. if fMenuHandle is a menu created with NewMenu => call DisposeMenu.
- // N.B.: you should never call GetMenu to create the MenuHandle, instead call MAGetMenu.
- if (IsManagedMenu(currentMenuHandle))// case 1
- ; // do nothing
- else if (IsAResource((Handle)currentMenuHandle))// case 2
- ReleaseResource((Handle)currentMenuHandle);
- else // case 3
- DisposeMenu(currentMenuHandle);
- currentMenuHandle = NULL;
- }
-
- // set the menu handle in the popup's private storage
- if (fCMgrControl && ((*fCMgrControl)->contrlData))
- {
- (*(popupPrivateDataHandle)(*fCMgrControl)->contrlData)->mHandle = itsMenuHandle;
- (*(popupPrivateDataHandle)(*fCMgrControl)->contrlData)->mID = (*itsMenuHandle)->menuID;
-
- // set up the control's needed values
- SetMin(1, kDontRedraw); // min == 1 always
- if (itsMenuHandle) // max == num items in menu
- SetMax(CountMItems(itsMenuHandle), kDontRedraw);
- else
- SetMax(0, kDontRedraw);
- SetVal(1, kDontRedraw); // val == current item
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal MenuHandle TPopup::GetMenuHandle(void)
- {
- if (fCMgrControl && (*fCMgrControl)->contrlData)
- return (*(popupPrivateDataHandle)(*fCMgrControl)->contrlData)->mHandle;
- else
- return NULL;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal CmdNumber TPopup::GetCurrentCommand(void)
- {
- return CmdFromMenuItem(GetMenuID(), GetCurrentItem());
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal short TPopup::GetCurrentItem(void)
- {
- long lastResult;
-
- lastResult = (*(popupPrivateDataHandle)(*fCMgrControl)->contrlData)->mLastResult;
- if (lastResult == 0)
- {
- if (!fCMgrControl)
- return 1;
- else
- return GetCtlValue(fCMgrControl);
- }
- else
- return LoWord(lastResult);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::GetItemText(short item,
- Str255& theText)
- {
- MenuHandle itsMenuHandle;
-
- theText = "";
- itsMenuHandle = GetMenuHandle();
- if (itsMenuHandle)
- GetItem(itsMenuHandle, item, theText);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal short TPopup::GetMenuID(void)
- {
- MenuHandle itsMenuHandle;
- long lastResult;
-
- lastResult = (*(popupPrivateDataHandle)(*fCMgrControl)->contrlData)->mLastResult;
- if (lastResult == 0)
- {
- itsMenuHandle = GetMenuHandle();
- if (!itsMenuHandle)
- return kNoResource;
- else
- return (*itsMenuHandle)->menuID;
- }
- else
- return HiWord(lastResult);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal short TPopup::GetNumberOfItems(void)
- {
- if (fCMgrControl)
- return GetCtlMax(fCMgrControl);
- else
- return 0;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal short TPopup::GetRsrcID(void)
- {
- return GetMenuID();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::GetTitle(Str255& title)
- {
- title = "";
- if (fCMgrControl)
- GetCTitle(fCMgrControl, title);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::SetCurrentItem(short item,
- Boolean redraw)
- {
- if (fCMgrControl)
- SetCtlValue(fCMgrControl, item);
- if (redraw)
- ForceRedraw();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::SetPopup(MenuHandle itsMenuHandle,
- short itsCurrentItem,
- Boolean redraw)
- {
- AttachMenuHandle(itsMenuHandle);
- SetCurrentItem(itsCurrentItem, redraw);
- if (redraw)
- ForceRedraw();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgRes
-
- pascal void TPopup::SetTitle(const Str255& title)
- {
- if (fCMgrControl)
- SetCTitle(fCMgrControl, title);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment DlgFields
-
- pascal void TPopup::Fields(TObject* obj) // override
- {
- obj->DoToField("TPopup", (Ptr)NULL, bClass);
-
- inherited::Fields(obj);
- }
-
-
-